<?php
class Delivery_Model_Mapper_Horarios
	extends Delivery_Model_Mapper_Abstract
{
	
	public function getDbTable()
	{
		return parent::getDbTable( 'Horarios' );
	}
	
	public function save( $horario ){
		
		$dbTable= $this->getDbTAble();
		
		if ( $horario->getId() == null ){		
			$horario->idHorario = $dbTable->insert( $horario );
		} else {
			$horario->idHorario = $dbTable->update( $horario );
		}
		
	}
	
	public function delete( $horario )
	{
		$dbTable= $this->getDbTAble();
		
		$this->logDeleted( $horario );
		
		$dbTable->delete( $horario );
	}
	
	/**
	 * @param int $idHorario
	 * @return object Delivery_Model_Horario
	 */
	public function getById( $idHorario ){
	
		if ( $this->getIdentityMap()->isRegistered( 'Delivery_Model_Horario', $idHorario )){
			return $this->getIdentityMap()->get( 'Delivery_Model_Horario', $idHorario );
		}
		
		$dbTable= $this->getDbTAble();
		$row	= $dbTable->getById( $idHorario );
	
		if( empty($row) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::HOUR_NOT_FOUND );
		}
		
		return $this->_fetch( $row );
	}	
	
	
	
	public function getByIds( $ids )
	{
		$dbTable= $this->getDbTAble();
		
		return $this->_fetchCollection(
			'Horarios',
			$dbTable->getByIds( $ids )
		);
	}
	
	public function getByNegocio( $idNegocio )
	{
		$dbTable= $this->getDbTAble();
		
		return $this->_fetchCollection(
			'Horarios',
			$dbTable->getByNegocio( $idNegocio )
		);
	}
		
	
	/**
	 * @param array
	 * @return object Delivery_Model_Horario
	 */
	protected function _fetch( $row )
	{
		$horario = new Delivery_Model_Horario( $row );
		
		$this->getIdentityMap()->set( $horario, $horario->getId() );
		
		return $horario;
	}
	
}